<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Order as OrderModel;
use app\common\model\Goods;
use app\web\controller\Express;
use think\Db;
use app\common\controller\Alipay;

/**
 * 技师控制器
 * @package app\index\controller
 */
class Statistics extends Auth
{
    public $noNeedLogin = [];

    public static $projectMoney = 0;//项目
    public static $projectCommission = 0;//项目提成
    public static $cardMoney = 0;//售卡金额
    public static $cardCommission = 0;//售卡提成
    public static $productMoney = 0;//商品金额
    public static $productCommission = 0;//商品提成
    public static $room_number = 0;//房间提成
    public static $user_number = 0;//客户数
    public static $averagePrice = 0;//客户均价
    public static $projectData = [];//项目数据
    public static $cardData = [];//卡数据
    public static $productData = [];//商品数据

    /**
     * 技师个人统计
     */
    public function userStatistics(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if($getData['type'] == '1') {
                $field = 'created_time';
            }else{
                $field = 'updated_time';
            }
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            $data = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id']])
                ->whereTime($field, $time)
                ->select();
            //处理数据
            foreach ($data as $key=>$value){
                //项目
                if($value['type'] == '1'){
                    self::$projectMoney += $value['money'];
                    self::$projectCommission += $value['commission'];
                    self::$projectData[] = $value;
                }
                //商品
                if($value['type'] == '2'){
                    self::$productMoney += $value['money'];
                    self::$productCommission += $value['commission'];
                    self::$productData[] = $value;
                }
                //售卡
                if($value['type'] == '3'){
                    self::$cardMoney += $value['money'];
                    self::$cardCommission += $value['commission'];
                    self::$cardData[] = $value;
                }
            }
            self::$projectData = $this->getData(self::$projectData,'1',$userInfo);
            self::$productData = $this->getData(self::$productData,'2',$userInfo);
            self::$cardData = $this->getData(self::$cardData,'3',$userInfo);
            //用户数量
            self::$user_number =  Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id']])
                ->whereTime('created_time', $time)
                ->group('member')
                ->count();
            //房间数量
            self::$room_number = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id']])
                ->whereTime('created_time', $time)
                ->group('room_id')
                ->count();
            //客单价
            $total = self::$projectMoney + self::$productMoney;
            if(self::$user_number == '0'){
                self::$averagePrice = '0';
            }else{
                self::$averagePrice = number_format(($total / self::$user_number),'2');
            }

            $result['projectMoney'] = self::$projectMoney;
            $result['projectCommission'] = self::$projectCommission;
            $result['cardMoney'] = self::$cardMoney;
            $result['cardCommission'] = self::$cardCommission;
            $result['productMoney'] = self::$productMoney;
            $result['productCommission'] = self::$productCommission;
            $result['room_number'] = self::$room_number;
            $result['user_number'] = self::$user_number;
            $result['averagePrice'] = self::$averagePrice;
            $result['projectData'] = self::$projectData;
            $result['cardData'] = self::$cardData;
            $result['productData'] = self::$productData;
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);

    }

    /**
     * 获取用户服务详细提成
     * @param $dataName
     * @param $type
     * @param $userInfo
     * @return array
     */
    public function getData($dataName,$type,$userInfo){
        $row = 0;//轮排
        $clock= 0;//点钟
        $call = 0;//call钟
        $add = 0;//加钟
        //列出服务项目
        foreach ($dataName as $key=>$value){
            if($type == '1'){
                $name = Db::name('qt_project')->where('id',$value['product_id'])->value('name');
            }
            if($type == '2'){
                $name = Db::name('qt_product')->where('id',$value['product_id'])->value('name');
            }
            if($type == '3'){
                $name = '会员卡';
            }

            $count = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id'],'type'=>$type,'product_id'=>$value['product_id']])
                ->count();
            $money = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id'],'type'=>$type,'product_id'=>$value['product_id']])
                ->sum('money');
            $commission = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id'],'type'=>$type,'product_id'=>$value['product_id']])
                ->sum('commission');
            $data['data'][$key]['name'] = $name;
            $data['data'][$key]['number'] = $count;
            $data['data'][$key]['money'] = $money;
            $data['data'][$key]['commission'] = $commission;
            if($value['status'] == '1'){
                $row += 1;
            }
            if($value['status'] == '2'){
                $clock += 1;
            }
            if($value['status'] == '3'){
                $call += 1;
            }
            if($value['status'] == '4'){
                $add += 1;
            }
        }
        $data['row'] = $row;
        $data['clock'] = $clock;
        $data['call'] = $call;
        $data['add'] = $add;
        return $data;
    }

    /**
     * 楼牌统计技师
     */
    public function storeUser(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if($getData['type'] == '1') {
                $field = 'created_time';
            }else{
                $field = 'updated_time';
            }
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //用户数据
            $userRes = Db::name('qt_commission_log')
                ->field('cl.id,cl.user_id,u.work_number')
                ->alias('cl')
                ->leftJoin('qt_users u','u.id = cl.user_id')
                ->where(['cl.store_id'=>$userInfo['store_id']])
                ->whereTime('cl.'.$field, $time)
                ->group('cl.user_id')
                ->select();
            //总数据
            $dataRes = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id']])
                ->whereTime($field, $time)
                ->select();
            //构造返回数据
            static $result = [];
            foreach ($userRes as $key=>$value){
                $result['data'][$value['user_id']]['user_id'] = $value['user_id'];
                $result['data'][$value['user_id']]['project'] = 0;
                $result['data'][$value['user_id']]['product'] = 0;
                $result['data'][$value['user_id']]['money'] = 0;
                $result['data'][$value['user_id']]['card'] = 0;
                $result['data'][$value['user_id']]['commission'] = 0;
                $result['data'][$value['user_id']]['work_number'] = $value['work_number'];
                $result['total']['project'] = 0;
                $result['total']['product'] = 0;
                $result['total']['money'] = 0;
                $result['total']['card'] = 0;
                $result['total']['commission'] = 0;

            }
            //数据填充
            //订单类型 1 轮排 2  点钟 3 call钟  4 优轮 5 加班
            foreach ($dataRes as $key=>$value){
                if($value['status'] == '1'){
                    $result['data'][$value['user_id']]['project'] += 1;
                    $result['total']['project']  += 1;
                }
                if($value['type'] == '2'){
                    $result['data'][$value['user_id']] ['product']+= 1;
                    $result['total']['product']  += 1;
                }
                if($value['type'] == '3'){
                    $result['data'][$value['user_id']]['card'] += 1;
                    $result['total']['card']  += 1;
                }
                $result['data'][$value['user_id']]['money'] += $value['money'];
                $result['total']['money']  += $value['money'];
                $result['data'][$value['user_id']]['commission'] += $value['commission'];
                $result['total']['commission']  += $value['commission'];
            }
            if(empty($result)){
                $result = [];
            }else{
                $result['data'] = array_values($result['data']);
            }
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 楼牌统计技师工作
     */
    public function storeUserWork(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if($getData['type'] == '1') {
                $field = 'created_time';
            }else{
                $field = 'updated_time';
            }
            if(isset($getData['start_time'])){
                $startTime = strtotime($getData['start_time']);
            }
            if(isset($getData['end_time'])){
                $endTime = strtotime($getData['end_time']);
            }
            //用户数据
            $userRes = Db::name('qt_commission_log')
                ->field('cl.id,cl.user_id,u.real_name')
                ->alias('cl')
                ->leftJoin('qt_users u','u.id = cl.cashier_id')
                ->where(['cl.store_id'=>$userInfo['store_id']])
                ->whereBetweenTime('created_time',$startTime,$endTime)
                ->group('cl.cashier_id')
                ->select();
            //总数据
            $dataRes = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id']])
                ->whereBetweenTime('created_time',$startTime,$endTime)
                ->select();
            //构造返回数据
            static $result = [];
            foreach ($userRes as $key=>$value){
                $result['data'][$value['user_id']]['user_id'] = $value['user_id'];
                $result['data'][$value['user_id']]['row'] = 0;
                $result['data'][$value['user_id']]['clock'] = 0;
                $result['data'][$value['user_id']]['call'] = 0;
                $result['data'][$value['user_id']]['card'] = 0;
                $result['data'][$value['user_id']]['add'] = 0;
                $result['data'][$value['user_id']]['add_work'] = 0;
                $result['data'][$value['user_id']]['product'] = 0;
                $result['data'][$value['user_id']]['real_name'] = $value['real_name'];
                $result['total']['row'] = 0;
                $result['total']['clock'] = 0;
                $result['total']['call'] = 0;
                $result['total']['add'] = 0;
                $result['total']['add_work'] = 0;
                $result['total']['product'] = 0;
            }
            //数据填充
            //订单类型 1 轮排 2  点钟 3 call钟  4 优轮 5 加钟 6 加班
            foreach ($dataRes as $key=>$value){
                if($value['status'] == '1' || $value['status'] == '4'){
                    $result['data'][$value['user_id']]['row'] += 1;
                    $result['total']['row']  += 1;
                }
                if($value['status'] == '2'){
                    $result['data'][$value['user_id']] ['clock']+= 1;
                    $result['total']['clock']  += 1;
                }
                if($value['status'] == '3'){
                    $result['data'][$value['user_id']]['call'] += 1;
                    $result['total']['call']  += 1;
                }
                if($value['status'] == '5'){
                    $result['data'][$value['user_id']]['add'] += 1;
                    $result['total']['add']  += 1;
                }
                if($value['status'] == '6'){
                    $result['data'][$value['user_id']]['add_word'] += 1;
                    $result['total']['add_word']  += 1;
                }
                if($value['type'] == '2'){
                    $result['data'][$value['user_id']]['product'] += $value['money'];
                    $result['total']['product']  += $value['money'];
                }
            }
            $result['data'] = array_values($result['data']);
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 楼牌统计技师
     */
    public function reception(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if($getData['type'] == '1') {
                $field = 'created_time';
            }else{
                $field = 'updated_time';
            }
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //用户数据
            $userRes = Db::name('qt_commission_log')
                ->field('cl.id,cl.cashier_id,u.real_name')
                ->alias('cl')
                ->leftJoin('qt_users u','u.id = cl.cashier_id')
                ->where(['cl.store_id'=>$userInfo['store_id']])
                ->whereTime('cl.'.$field, $time)
                ->group('cl.cashier_id')
                ->select();
            //总数据
            $dataRes = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id']])
                ->whereTime($field, $time)
                ->select();
            //构造返回数据
            static $result = [];
            foreach ($userRes as $key=>$value){
                $result['data'][$value['cashier_id']]['user_id'] = $value['cashier_id'];
                $result['data'][$value['cashier_id']]['project'] = 0;
                $result['data'][$value['cashier_id']]['product'] = 0;
                $result['data'][$value['cashier_id']]['money'] = 0;
                $result['data'][$value['cashier_id']]['card'] = 0;
                $result['data'][$value['cashier_id']]['commission'] = 0;
                $result['data'][$value['cashier_id']]['real_name'] = $value['real_name'];
                $result['total']['project'] = 0;
                $result['total']['product'] = 0;
                $result['total']['money'] = 0;
                $result['total']['card'] = 0;
                $result['total']['commission'] = 0;
            }
            //数据填充
            //订单类型 1 轮排 2  点钟 3 call钟  4 优轮 5 加班
            foreach ($dataRes as $key=>$value){
                if($value['status'] == '1'){
                    $result['data'][$value['cashier_id']]['project'] += 1;
                    $result['total']['project']  += 1;
                }
                if($value['type'] == '2'){
                    $result['data'][$value['cashier_id']] ['product']+= 1;
                    $result['total']['product']  += 1;
                }
                if($value['type'] == '3'){
                    $result['data'][$value['cashier_id']]['card'] += 1;
                    $result['total']['card']  += 1;
                }
                $result['data'][$value['cashier_id']]['money'] += $value['money'];
                $result['total']['money']  += $value['money'];
            }
            if(empty($result)){
                $this->result('查询成功', $result, 200);
            }
            $result['data'] = array_values($result['data']);
            $this->result('查询成功', $result, 200);
        }
    }

    /**
     * 楼面提成明细
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function receptionDetail(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            if($getData['type'] == '1') {
                $field = 'created_time';
            }else{
                $field = 'updated_time';
            }
            $startTime = strtotime($getData['start_time']);
            $endTime = strtotime($getData['end_time']);
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            $projectData = Db::name('qt_commission_log')
                ->field('p.name as project_name,p.price,p.payment_price,p.created_time,log.receiver,r.name as room_name')
                ->alias('log')
                ->leftJoin('qt_order_project p','log.order_project_id = p.id')
                ->leftJoin('qt_room r','r.id = p.room_id')
                ->where(['cashier_id'=>$getData['user_id'],'type'=>'1'])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->select();
            $goodsData = Db::name('qt_commission_log')
                ->field('item.goods_name as project_name,item.goods_total_price as price,item.payment_price,item.created_time,log.receiver,r.name as room_name')
                ->alias('log')
                ->leftJoin('qt_product_order_item item','log.order_project_id = item.id')
                ->leftJoin('qt_room r','r.id = item.room_id')
                ->where(['cashier_id'=>$getData['user_id'],'type'=>'2'])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->select();
            $result['project'] = array_merge($projectData,$goodsData);
            //会员卡
            $cardData = Db::name('qt_commission_log')
                ->field('card.name as card_name,recharge.amount,recharge.give_amount,log.created_time,card.card_no,log.receiver')
                ->alias('log')
                ->leftJoin('qt_viper_card_recharge_log recharge','log.order_project_id = recharge.id')
                ->leftJoin('qt_viper_card card','card.id = recharge.viper_card_id')
                ->where(['cashier_id'=>$getData['user_id']])
                ->whereIn('log.type',[3,4,5,7])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->select();
            $result['card'] = $cardData;
            $this->result('查询成功', $result, 200);
        }
    }



    /**
     * 门店业绩
     */
    public function store(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //总金额
            $result['totalPrice'] = Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('updated_time', $time)->sum('money');
            //现金
            $result['totalCash'] = Db::name('qt_commission_log')
                ->where('pay_type','4')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
            //其他收入
            $result['totalOther']  = Db::name('qt_commission_log')
                ->where('pay_type','5')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
            //现金消费
            $result['totalCashConsumption']  = Db::name('qt_commission_log')
                ->where('pay_type','5')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('payment_price');
            //售卡业绩
            $result['totalCard'] =  Db::name('qt_commission_log')
                ->where('type','3')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
            //新增会员
            $result['totalAddUser'] = Db::name('qt_user_viper')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('create_time', $time)
                ->count();
            //商品业绩
            $result['totalProduct'] =  Db::name('qt_commission_log')
                ->where('type','2')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
            //消费人数
            $result['totalUser'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->group('member')
                ->count();
            if($result['totalUser'] == '0' || $result['totalPrice'] == '0'){
                $result['totalUserAverage'] = "0";
            }else{
                //人均
                $result['totalUserAverage']  = number_format(($result['totalPrice'] / $result['totalUser']),2);
            }
            //会员消费数
            $result['totalUserConsumption'] =  Db::name('qt_commission_log')
                ->field('cl.*')
                ->alias('cl')
                ->leftJoin('qt_user_viper uv','uv.id = cl.user_id')
                ->where('cl.store_id',$userInfo['store_id'])
                ->where('uv.store_id','1')
                ->whereTime('cl.updated_time', $time)
                ->sum('payment_price');

            //会员续费
            $result['totalUserRenew'] =  Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_user_viper uv','uv.id = cl.user_id')
                ->where('cl.store_id',$userInfo['store_id'])
                ->where('cl.type','4')
                ->whereTime('cl.updated_time', $time)
                ->sum('payment_price');
            //赠送金额
            $result['totalGiveMoney'] =  Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_user_viper uv','uv.id = cl.user_id')
                ->where('cl.store_id',$userInfo['store_id'])
                ->where('cl.type','in',['3','4'])
                ->whereTime('cl.updated_time', $time)
                ->sum('give_money');

            //项目提成
            $result['projectCommission'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->where('type','1')
                ->whereTime('updated_time', $time)
                ->sum('commission');

            //项目业绩
            $result['projectAchievement'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->where('type','1')
                ->whereTime('updated_time', $time)
                ->sum('payment_price');

            //接待客服
            $result['roomCount'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->where('type','in',['3','4'])
                ->whereTime('updated_time', $time)
                ->group('room_id')
                ->count();

            //售卡提成
            $result['cardCommission'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->where('type','3')
                ->whereTime('updated_time', $time)
                ->sum('commission');

            $this->result('查询成功', $result, 200);
        }
    }

    /**
     * 统计图表
     */
    public function storeChart(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //总金额
            $result['total'] = $this->getMoneyTotal($userInfo['store_id'],$time,'1');
            /****************** 其他收入 ***********************/
            $result['other'] = $this->getMoneyTotal($userInfo['store_id'],$time,'2');
            /****************** 柱状图 ***********************/
            $result['histogram'] = $this->histogram($result['total']['series']['data']);
            /****************** 会员充值 ***********************/
            $result['userRecharge'] = $this->getCardTotal($userInfo['store_id'],$time,'1');
            /****************** 会员消费 ***********************/
            $result['userConsumption'] = $this->getCardTotal($userInfo['store_id'],$time,'2');
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 获取消费数据统计
     * @param $storeId[门店ID]
     * @param $time[时间类型] [1 今天 2 昨天 3 本月 4 上月]
     * @param $type [类型] [1营收 2 非营收]
     */
    public function getMoneyTotal($storeId,$time,$type){
        if($type == '2'){
            $where['type'] = '5';
        }else{
            $where = 1;
        }
        //总金额
        $result['totalPrice'] = Db::name('qt_commission_log')
            ->where('store_id',$storeId)
            ->where($where)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //现金
        $result['totalCash'] = Db::name('qt_commission_log')
            ->where('pay_type','4')
            ->where($where)
            ->where('store_id',$storeId)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //微信
        $result['totalWechat']  = Db::name('qt_commission_log')
            ->where('pay_type','1')
            ->where($where)
            ->where('store_id',$storeId)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //支付宝
        $result['totalAlipay']  = Db::name('qt_commission_log')
            ->where('pay_type','2')
            ->where($where)
            ->where('store_id',$storeId)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //会员卡
        $result['totalCard']  = Db::name('qt_commission_log')
            ->where('pay_type','3')
            ->where($where)
            ->where('store_id',$storeId)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //优惠金额
        $result['totalCoupon']  = Db::name('qt_commission_log')
            ->where('store_id',$storeId)
            ->where($where)
            ->whereTime('updated_time', $time)
            ->sum('coupon_money');
        $data[0]['name'] = '支付宝';
        $data[0]['value'] = $result['totalAlipay'];
        $data[1]['name'] = '微信';
        $data[1]['value'] =  $result['totalWechat'];
        $data[2]['name'] = '现金';
        $data[2]['value'] =  $result['totalCash'];
        $data[3]['name'] = '会员卡';
        $data[3]['value'] =  $result['totalCard'];
        $data[4]['name'] = '优惠券';
        $data[4]['value'] =  $result['totalCoupon'];
        $res['total'] =  $result['totalPrice'];
        $res['series']['data'] = $data;
        return $res;
    }

    /**
     * 获取门店项目详情
     */
    public function storeProject(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if($getData['type'] == '1'){
                $type = 'cl.created_time';
            }
            if($getData['type'] == '2'){
                $type = 'cl.updated_time';
            }
            $groupRes = Db::name('qt_commission_log')
                ->field('cl.*，p.name')
                ->alias('cl')
                ->leftJoin('qt_project p','cl.product_id = p.id')
                ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'1'])
                ->whereTime($type,$time)
                ->group('cl.product_id')
                ->select();
            $result = [];

            foreach ($groupRes as $key=>$value){
                $result[$key]['name'] = $value['name'];
                $dataRes = Db::name('qt_commission_log')
                    ->alias('cl')
                    ->leftJoin('qt_project p','cl.product_id = p.id')
                    ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'1','cl.product_id'=>$value['product_id']])
                    ->whereTime($type,$time)
                    ->select();
                $result[$key]['number'] = count($dataRes);
                $result[$key]['row']  = 0;
                $result[$key]['clock']  = 0;
                $result[$key]['call']  = 0;
                $result[$key]['add']  = 0;
                $result[$key]['money']  = 0;
                foreach ($dataRes as $k=>$v){
                    $result[$key]['money'] += $v['money'];
                    if($v['status'] == '1'){
                        $result[$key]['row'] += 1;
                    }
                    if($v['status'] == '2'){
                        $result[$key]['clock'] += 1;
                    }
                    if($v['status'] == '3'){
                        $result[$key]['call'] += 1;
                    }
                    if($v['status'] == '4'){
                        $result[$key]['add'] += 1;
                    }
                }
                $this->result('查询成功','',200);

            }
        }
    }


    /**
     * 店铺商品统计
     */
    public function storeProduct(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if($getData['type'] == '1'){
                $type = 'cl.created_time';
            }
            if($getData['type'] == '2'){
                $type = 'cl.updated_time';
            }
            $groupRes = Db::name('qt_commission_log')
                ->field('cl.*，p.name')
                ->alias('cl')
                ->leftJoin('qt_project p','cl.product_id = p.id')
                ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'2'])
                ->whereTime($type,$time)
                ->group('cl.product_id')
                ->select();
            $result = [];
            foreach ($groupRes as $key=>$value){
                $result[$key]['name'] = $value['name'];
                $dataRes = Db::name('qt_commission_log')
                    ->alias('cl')
                    ->leftJoin('qt_product p','cl.product_id = p.id')
                    ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'1','cl.product_id'=>$value['product_id']])
                    ->whereTime($type,$time)
                    ->select();
                $result[$key]['number'] = count($dataRes);
                $result[$key]['row']  = 0;
                $result[$key]['clock']  = 0;
                $result[$key]['call']  = 0;
                $result[$key]['add']  = 0;
                $result[$key]['money']  = 0;
                foreach ($dataRes as $k=>$v){
                    $result[$key]['money'] += $v['money'];
                    if($v['status'] == '1'){
                        $result[$key]['row'] += 1;
                    }
                    if($v['status'] == '2'){
                        $result[$key]['clock'] += 1;
                    }
                    if($v['status'] == '3'){
                        $result[$key]['call'] += 1;
                    }
                    if($v['status'] == '4'){
                        $result[$key]['add'] += 1;
                    }
                }
                $this->result('查询成功','',200);
            }
        }
    }

    /**
     * 获取售卡统计
     */
    public function storeCard(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if($getData['type'] == '1'){
                $type = 'cl.created_time';
            }
            if($getData['type'] == '2'){
                $type = 'cl.updated_time';
            }
            $groupRes = Db::name('qt_commission_log')
                ->field('cl.*，p.name')
                ->alias('cl')
                ->leftJoin('qt_viper_card c','cl.product_id = c.id')
                ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'3'])
                ->whereTime($type,$time)
                ->group('cl.product_id')
                ->select();
            $result = [];
            foreach ($groupRes as $key=>$value){
                $result[$key]['name'] = $value['name'];
                $dataRes = Db::name('qt_commission_log')
                    ->alias('cl')
                    ->leftJoin('qt_product p','cl.product_id = p.id')
                    ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'1','cl.product_id'=>$value['product_id']])
                    ->whereTime($type,$time)
                    ->select();
                $result[$key]['number'] = count($dataRes);
                $result[$key]['row']  = 0;
                $result[$key]['clock']  = 0;
                $result[$key]['call']  = 0;
                $result[$key]['add']  = 0;
                $result[$key]['money']  = 0;
                foreach ($dataRes as $k=>$v){
                    $result[$key]['money'] += $v['money'];
                    if($v['status'] == '1'){
                        $result[$key]['row'] += 1;
                    }
                    if($v['status'] == '2'){
                        $result[$key]['clock'] += 1;
                    }
                    if($v['status'] == '3'){
                        $result[$key]['call'] += 1;
                    }
                    if($v['status'] == '4'){
                        $result[$key]['add'] += 1;
                    }
                }
                $this->result('查询成功',$result,200);
            }
        }
    }


    /**
     * 会员卡消费统计
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function storeUserConsumption(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if($getData['type'] == '1'){
                $type = 'cl.created_time';
            }
            if($getData['type'] == '2'){
                $type = 'cl.updated_time';
            }
            $groupRes = Db::name('qt_commission_log')
                ->field('cl.*，p.name')
                ->alias('cl')
                ->leftJoin('qt_viper_card c','cl.product_id = c.id')
                ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'3'])
                ->whereTime($type,$time)
                ->group('cl.product_id')
                ->select();
            $result = [];
            foreach ($groupRes as $key=>$value){
                $result[$key]['name'] = $value['name'];
                $dataRes = Db::name('qt_commission_log')
                    ->alias('cl')
                    ->leftJoin('qt_product p','cl.product_id = p.id')
                    ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'1','cl.product_id'=>$value['product_id']])
                    ->whereTime($type,$time)
                    ->select();
                $result[$key]['number'] = count($dataRes);
                $result[$key]['row']  = 0;
                $result[$key]['clock']  = 0;
                $result[$key]['call']  = 0;
                $result[$key]['add']  = 0;
                $result[$key]['money']  = 0;
                foreach ($dataRes as $k=>$v){
                    $result[$key]['money'] += $v['money'];
                    if($v['status'] == '1'){
                        $result[$key]['row'] += 1;
                    }
                    if($v['status'] == '2'){
                        $result[$key]['clock'] += 1;
                    }
                    if($v['status'] == '3'){
                        $result[$key]['call'] += 1;
                    }
                    if($v['status'] == '4'){
                        $result[$key]['add'] += 1;
                    }
                }
                $this->result('查询成功',$result,200);
            }
        }
    }

    /**
     * 获取柱状图
     * @param $data
     */
    public function histogram($data){
        foreach ($data as $key=>$value){
            $categories[$key] = $value['name'];
            $money[$key] = $value['value'];
        }
        $result['histogram']['categories'] = $categories;
        $result['histogram']['name'] = '金额';
        $result['histogram']['data'] = $money;
        return $result;
    }


    /**
     * 获取会员卡数据统计
     * @param $storeId[门店ID]
     * @param $time[时间类型] [1 今天 2 昨天 3 本月 4 上月]
     * @param $type [类型] [1会员充值 2 会员消费]
     */
    public function getCardTotal($storeId,$time,$type){
        //会员充值
        if($type == '1'){
            $in = ['3','4'];
        }
        //会员消费
        if($type == '2'){
            $in = ['1','2','5'];
        }
        //会员充值
        $result['totalCardPrice'] = Db::name('qt_commission_log')
            ->where('store_id',$storeId)
            ->where('type','in',['3','4'])
            ->whereTime('updated_time', $time)
            ->sum('money');
        $careType = Db::name('qt_viper_type')->where(['store_id'=>$storeId])->select();
        foreach ($careType as $key=>$value){
            $result['series'][$key]['name'] = $value['name'];
            $result['series'][$key]['value'] = Db::name('qt_commission_log')
                ->where('store_id',$storeId)
                ->where('type','in',$in)
                ->where('card_type_id',$value['id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
        }
        return $result;
    }

}
